<?php
class main extends spController
{
	function __construct()
	{
		parent::__construct();
		spClass('lib_user')->checklogin();
		$this->userinfo = false;
		if (isset($_SESSION['userinfo'])) $this->userinfo = $_SESSION['userinfo'];
		$this->topnews = spClass("lib_contents")->findAll(array("type" => 'post'), "id desc", null, '0, 10');
		$this->topcomments = spClass("lib_category")->getTopComments(10);
		$this->category = spClass('lib_category')->getCategory(1);
		$this->archive = spClass('lib_category')->getArchive();
	} 

	function test()
	{
		$cate = spClass('lib_category')->getComments(9);
		dump($cate);
	} 

	function index() // 这里是首页
	{
		$this->news = spClass("lib_category")->getContents();

		$this->display("front/index1.html");
	} 

	function category() // 这里是首页
	{
		$this->news = spClass("lib_category")->getContents($this->spArgs('id'));
		$this->display("front/index1.html");
	} 

	function article() // 查看文章
	{
		if ($id = $this->spArgs("id"))
		{
			$this->id = $id;
			$this->anchor = $this->spArgs("anchor", "");
			$this->post = spClass("lib_contents")->find(array('id' => $id));
			$count = spClass("lib_contents");
			$upconditions = array("id" => $id);
			$upcount = 'count';
			$count->incrField($upconditions, $upcount, 1);
			$this->comments = spClass("lib_category")->getComments($id); //dump($this->comments );
			$this->commentscount = count($this->comments);
			$this->display("front/article.html");
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("main", "index"));
		} 
	} 

	function search() // 这里是首页
	{
		$s = $this->spArgs('s');
		$this->title = '搜索结果';
		$this->news = spClass("lib_category")->getContents(false, $s);
		$this->display("front/index1.html");
	} 

	function archive() // 这里是首页
	{
		$d = $this->spArgs('d');
		$pos = strpos($d, '-');
		$this->title = substr($d, 0, $pos) . '年' . substr($d, $pos + 1) . '月文章归档';
		$this->news = spClass("lib_category")->getArchives($d);
		$this->display("front/index1.html");
	} 

	function page()
	{ 
		// 1、显示page页面内容,这里先判断是否传入了id
		if ($id = $this->spArgs("id"))
		{ 
			// 查找表，获取信息
			$this->page = spClass("lib_contents")->find(array('id' => $id)); 
			// 实现文章浏览次数加一
			$count = spClass("lib_contents");
			$upconditions = array("id" => $id);
			$upcount = 'count';
			$count->incrField($upconditions, $upcount, 1);
		} 
		else
		{ 
			// 无id则直接跳转回首页
			$this->jump(spUrl("main", "index"));
		} 
		$this->display("front/pages.html");
	} 

	function change() // 浏览分类下文章
	{
		// 第一步先通过分类slug取得分类id
		$slug = $this->spArgs("slug"); // 用spArgs接收spUrl传过来的分类slug
		$cateslug = spClass("lib_category")->find(array('slug' => $slug)); // 获取slug对应的分类         
		// echo "<p>留言标题：{$cateslug['title']}</p>";
		$id = $cateslug['id']; 
		// 第二步再通过分类id来获取分类下文章列表
		$post = spClass("lib_contents");
		$this->results = $post->spPager($this->spArgs('page', 1), 20)->findAll(array("type" => 'post', "parent" => $id), "id desc", null, null);
		$this->select = spClass("lib_category")->findAll(); 
		// 这里获取分页数据并发送到smarty模板内
		$this->pager = $post->spPager()->getPager();
		$this->display("front/change.html");
	} 

	function commentsave()
	{
		$row = $_POST;
		$row['comment_date'] = date('Y-m-d H:i:s');
		if (spClass('lib_comments')->create($row))
			echo json_encode(array('msg' => 'OK'));
		else
			echo json_encode(array('msg' => 'NO'));
		exit;
	} 

	function checkacl()
	{

		if (false == $this->userinfo)
			echo json_encode(array('msg' => 'NO'));
		else
			echo json_encode(array('msg' => 'OK'));
		exit;
	} 

	function login()
	{
		$name = $this->spArgs('username');
		$password = md5($this->spArgs('password'));
		$remember = $this->spArgs('remember');
		if (spClass('lib_user')->userlogin($name, $password, $remember))
		{
			$msg = array('msg' => 'OK',
				'screenname' => $_SESSION['userinfo']['screenname'],
				'lastlogin' => $_SESSION['userinfo']['lastlogin'],
				'logincount' => $_SESSION['userinfo']['logincount']);
			echo json_encode($msg);
		} 
		else
		{
			spAccess('w', 'qqqq', $password);
			echo json_encode(array('msg' => 'NO'));
		} 
		exit;
	} 
} 
